package com.dlc.shop.user.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.dto.ShopGrowthGainDTO;
import com.dlc.shop.bean.model.Order;
import com.dlc.shop.common.util.Arith;
import com.dlc.shop.user.common.dao.ShopGrowthConfigMapper;
import com.dlc.shop.user.common.model.ShopGrowthConfig;
import com.dlc.shop.user.common.service.ShopGrowthConfigService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 店铺成长值配置
 *
 * @author lhd
 * @date 2022-10-17 17:23:24
 */
@Service
@AllArgsConstructor
public class ShopGrowthConfigServiceImpl extends ServiceImpl<ShopGrowthConfigMapper, ShopGrowthConfig> implements ShopGrowthConfigService {

    private final ShopGrowthConfigMapper brandGrowthConfigMapper;

    @Override
    public Integer calGrowth(Order order, double totalAmount) {
        // 查询当前店铺的成长值配置
        ShopGrowthConfig growthConfig = this.getOne(new LambdaQueryWrapper<ShopGrowthConfig>()
                .eq(ShopGrowthConfig::getShopId, order.getShopId()));
        if (growthConfig == null
                || ObjectUtils.isEmpty(growthConfig.getGrowthGain())) {
            return 0;
        }
        // 计算成长值
        ShopGrowthGainDTO growthGainDTO = JSONObject.parseObject(growthConfig.getGrowthGain(), ShopGrowthGainDTO.class);
        if (!growthGainDTO.getShopGrowthSwitch()) {
            return 0;
        }
        Integer growth = growthGainDTO.getBuyOrder();
        growth += (int) Arith.div(totalAmount,growthGainDTO.getBuyPrice());
        return growth;
    }
}
